package pe.edu.upc.dew.grupo3a.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hamcrest.core.IsNull;
import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo3a.model.Funcion;
import pe.edu.upc.dew.grupo3a.model.Pelicula;
import pe.edu.upc.dew.grupo3a.model.Sala;
import pe.edu.upc.dew.grupo3a.service.FuncionService;
import pe.edu.upc.dew.grupo3a.service.PeliculaService;
import pe.edu.upc.dew.grupo3a.service.SalaService;

/**
 * Servlet implementation class IndexServlet
 */
public class RedirectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService  peliculaService;
	private SalaService salaService;
	private FuncionService funcionService;
	
    /**
     * Default constructor. 
     */
    public RedirectServlet() {
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public void init(ServletConfig config) throws ServletException	{
			this.peliculaService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("peliculaService", PeliculaService.class);
			//
			this.salaService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("salaService", SalaService.class);
			//
			this.funcionService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("funcionService", FuncionService.class);
			
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		List<Pelicula> peliculasEstreno = peliculaService.getPeliculasPorEstado("Estreno");
		List<Pelicula> peliculasPorEstrenar = peliculaService.getPeliculasPorEstado("Proximamente");
		request.setAttribute("peliculasEstreno", peliculasEstreno);
		request.setAttribute("peliculas", peliculasEstreno);
		request.setAttribute("peliculasPorEstrenar", peliculasPorEstrenar);
		//
		HttpSession session = request.getSession();

		// Get Page For redirect
		String page = request.getParameter("page");
		if ( page != null){
			if((page.equals("registroFuncion") ||page.equals("registropelicula")) && session.getAttribute("AdministradorActivo")==null)
				page="Login";
			if(page.equals("entradas") && session.getAttribute("ClienteActivo")==null)
				page="Login";

			if(page.equals("registroFuncion")){
				List<Sala> salas = salaService.getSalas();
				request.setAttribute("salas", salas);
			}
			if(page.equals("entradas")){
				List<Funcion> funciones = funcionService.getFuncionPorPelicula(peliculasEstreno.get(0).getNombre());
				request.setAttribute("funciones", funciones);
			}
			request.getRequestDispatcher(page+".jsp").forward(request,response);
		}
		else 
			request.getRequestDispatcher("home.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
